package im.zego.zimexample.adapter;

import android.os.Handler;
import android.text.Html;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import im.zego.zimexample.adapter.holder.ChatLeftViewHolder;
import im.zego.zimexample.adapter.holder.ChatRightViewHolder;
import im.zego.zimexample.emus.ChatItemType;
import im.zego.zimexample.entity.MessageInfo;

public class ChatContentAdapter extends RecyclerView.Adapter {
    private OnItemClickListener onItemClickListener;
    public Handler handler;
    private List<MessageInfo> messageInfoList = new ArrayList<>();

    public ChatContentAdapter() {

    }

    public void addItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (ChatItemType.CHAT_ITEM_TYPE_RIGHT.value == viewType) {
            viewHolder = new ChatRightViewHolder(parent, onItemClickListener, handler);
        } else if (ChatItemType.CHAT_ITEM_TYPE_LEFT.value == viewType) {
            viewHolder = new ChatLeftViewHolder(parent, onItemClickListener, handler);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        TextView contentTextView = null;
        TextView userIDTextView = null;

        if (holder instanceof ChatLeftViewHolder) {
            ChatLeftViewHolder chat = (ChatLeftViewHolder) holder;
            contentTextView = chat.contentTextView;
            userIDTextView = chat.userIDTextView;
            if (messageInfoList.get(position).getUserID() != null || !"".equals(messageInfoList.get(position).getUserID()) || messageInfoList.get(position).getUserID().length() == 0) {
                userIDTextView.setText(messageInfoList.get(position).getUserID());
            }
        } else if (holder instanceof ChatRightViewHolder) {
            ChatRightViewHolder chat = (ChatRightViewHolder) holder;
            contentTextView = chat.contentTextView;
            userIDTextView = chat.userIDTextView;
            if (messageInfoList.get(position).getUserID() != null || !"".equals(messageInfoList.get(position).getUserID()) || messageInfoList.get(position).getUserID().length() == 0) {
                userIDTextView.setText(messageInfoList.get(position).getUserID());
            }
        }

        contentTextView.setText(Html.fromHtml(messageInfoList.get(position).getContent()));

    }


    @Override
    public int getItemViewType(int position) {
        return messageInfoList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return messageInfoList.size();
    }


    public void add(MessageInfo messageInfo) {
        messageInfoList.add(messageInfo);
    }

    public void addAll(List<MessageInfo> list) {
        if (list != null) {
            messageInfoList.addAll(list);
        }
    }


    public MessageInfo getMessage(int index) {
        if (messageInfoList != null && messageInfoList.size() > 0) {
            return messageInfoList.get(index);
        }
        return null;
    }
}
